import java.io.Serializable;
import java.util.*;

public class CompteBancaire implements Serializable {

	private static final long serialVersionUID = -2062994542717559274L;

	private Titulaire m_titulaire;
	private long m_numero;
	private float m_solde;
	protected LinkedHashMap<Long, OperationBancaire> m_pOperation;
	private Banque m_banque;

	public CompteBancaire(final Banque b, Titulaire titulaire) {

		m_titulaire = titulaire;
		m_numero = b.numeroRegistreCpt();
		m_solde = 0f;
		m_pOperation = new LinkedHashMap<Long, OperationBancaire>();
		m_banque = b;
	}

	public Titulaire getTitulaireCompte() {

		return m_titulaire;
	}

	public long getNumeroCompte() {

		return m_numero;
	}

	public float getSoldeCompte() {

		return m_solde;
	}

	public float executer(OperationBancaire type) {

		if(type.getType() == 'R')
			this.sub(type.getMontant());

		if(type.getType() == 'V' || type.getType() == 'I')
			this.add(type.getMontant());

		m_pOperation.put(type.getNumero(), type);

		return m_solde;
	}

	public float executer(String intitule, char type, float montant) {

		return executer(new OperationBancaire(m_banque, type, intitule, montant));
	}

	protected void add(float montant) {

		m_solde += montant;
	}

	protected void sub(float montant) {

		m_solde -= montant;
	}

	public int getOperationDate(final String date) {

		int retour = 0;
		for (OperationBancaire op : m_pOperation.values()) {

			if (op.getDate().equals(date))
				retour++;
		}
		return retour;
	}


}